//
// DefaultIRQAction.cpp
// Usage: Implement of DefaultIRQAction
// Create Date: 2009-02-17
// Creator: MaJiuyue
//
#include <NewLife.h>
#include <io.h>
#include <SCI.h>
#include <RequestPackage.h>
#include "DefaultIRQAction.h"
#include "../core/ResourceManager.h"
#include "../ipc/MessageBox.h"
#include "../schedule/Process.h"

void DefaultIRQAction::operator() (u32 irq, regs_t *regs)
{
	// Prepare IRQ message
	size_t size = sizeof(MESSAGE)+sizeof(RequestPackage);
	MESSAGE *msg = (MESSAGE *)malloc(size);
	msg->size = size;
	RequestPackage *IRP = (RequestPackage *)(&msg->dat);
	IRP->PrepareIRQ(irq);

	// Send message to target process
	Resource * res = ResourceManager.RequestResource(
		ServiceCallInterface::IPCCenter::ResourceName
	);
	res->CallSCI(
		ServiceCallInterface::IPCCenter::SCIID_SendKNL,
		(u32)pid,
		(u32)msg
	);

	// If need reopen IRQ
	if(flags&SA_REOPEN_IRQ)
	{
		Resource * res = ResourceManager.RequestResource(
			ServiceCallInterface::IntrGateManager::ResourceName
		);
		res->CallSCI(
			ServiceCallInterface::IntrGateManager::SCIID_EnableIRQ,
			irq
		);
	}

	res = ResourceManager.RequestResource(
		ServiceCallInterface::ProcessManager::ResourceName
	);
	Process * proc = (Process *)res->CallSCI(
		ServiceCallInterface::ProcessManager::SCIID_GetProcessByPid,
		pid
	);

	res = ResourceManager.RequestResource(
		ServiceCallInterface::Scheduler::ResourceName
	);
	res->CallSCI(
		ServiceCallInterface::Scheduler::SCIID_SelectScheduleObject,
		(u32)proc
	);
}

